package com.tarena.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tarena.entity.Emp;
import com.tarena.entity.User;
import com.tarena.service.EmpService;

/**
 * liubiao
 */
@Controller
@RequestMapping("/emp")
public class EmpController {
	@Resource
	private EmpService empService;
	private PrintWriter out;

	//增加员工
	@RequestMapping("/saveEmp.do")
	public void save(Emp emp, HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		System.out.println("save");
		System.out.println(emp.getName());
		System.out.println("phone:" + emp.getPhone());
		boolean save = empService.save(emp);
		out = res.getWriter();
		if (save) {
			out.write("1");
			out.flush();
			out.close();
		} else {
			out.write("0");
			out.flush();
			out.close();
		}
	}

	//删除员工
	@RequestMapping("/deleteEmp.do")
	public void delete(HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		System.out.println("delete");
		boolean delete = empService.delete(Integer.parseInt(req.getParameter("id")));
		out = res.getWriter();
		if (delete) {
			out.write("1");
			out.flush();
			out.close();
		} else {
			out.write("0");
			out.flush();
			out.close();
		}
	}

	//修改员工
	@RequestMapping("/updateEmp.do")
	public void update(Emp emp, HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		out = res.getWriter();
		try {
			empService.update(emp);
			out.write("1");
			out.flush();
			out.close();
		} catch (Exception e) {
			out.write("0");
			out.flush();
			out.close();
		}
		
	}

	//查找所有员工
	@RequestMapping("/findEmp.do")
	public String findAll(Model mode) {
		List<Emp> list = empService.findAllEmp();
		mode.addAttribute("emps", list);
		return "emp/emp_list";
	}

	//通过ID查找员工
	@RequestMapping("/findById.do")
	public void findById(HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		System.out.println("aaaaaaa");
		User user = (User)req.getSession().getAttribute("user_online");
		System.out.println(user.getEmpId());
		List<Emp> list = empService.findById(user.getEmpId());
		System.out.println(list.get(0).getName());
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		jsonMap.put("total", 1);//total键 存放总记录数，必须的  
        jsonMap.put("rows", list);//rows键 存放每页记录 list  

        JSONObject result = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject
        out = res.getWriter();
		out.write(result.toString());
		System.out.println(result.toString());
		out.flush();
		out.close();
	}
	
	//分页查找员工
	@RequestMapping("/findByPage.do")
	public void findByPage(Emp emp, HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		String page = req.getParameter("page");
		String rows = req.getParameter("rows");
		System.out.println("findByPage");
		
		System.out.println(emp.getNo());
		System.out.println(emp.getName());
		List<Emp> list = null;
		//两者都不为null或两者不同时为""
		if ((null != emp.getNo() && null != emp.getName())){
			if ("".equals(emp.getName())&&"".equals(emp.getNo())){
				System.out.println("000000000000000000000000");
				System.out.println("no="+emp.getNo());
				System.out.println("name="+emp.getName());
				list = empService.findByPage(
						Integer.parseInt(rows) * (Integer.parseInt(page) - 1),
						Integer.parseInt(rows));
			}
			if (!"".equals(emp.getNo())&&"".equals(emp.getName())) {
				emp.setNo("%" + emp.getNo() + "%");
				System.out.println("111111111111111111111111111");
				System.out.println("no="+emp.getNo());
				System.out.println("name="+emp.getName());
				list = empService.findByPage(
					Integer.parseInt(rows) * (Integer.parseInt(page) - 1),
					Integer.parseInt(rows), emp);
			}
			if ("".equals(emp.getNo())&&!"".equals(emp.getName())) {
				emp.setName("%" + emp.getName() + "%");
				System.out.println("222222222222222222222222222");
				System.out.println("no="+emp.getNo());
				System.out.println("name="+emp.getName());
				list = empService.findByPage(
						Integer.parseInt(rows) * (Integer.parseInt(page) - 1),
						Integer.parseInt(rows), emp);
			}
			if (!"".equals(emp.getNo())&&!"".equals(emp.getName())) {
				emp.setNo("%" + emp.getNo() + "%");
				emp.setName("%" + emp.getName() + "%");
				System.out.println("333333333333333333333333333");
				System.out.println("no="+emp.getNo());
				System.out.println("name="+emp.getName());
				list = empService.findByPage(
						Integer.parseInt(rows) * (Integer.parseInt(page) - 1),
						Integer.parseInt(rows), emp);
			}
			
		} 
		//两者都为null(说明是第一次加载查询)
		else {
			System.out.println("4444444444444444444444444444444");
			System.out.println("no="+emp.getNo());
			System.out.println("name="+emp.getName());
			list = empService.findByPage(
					Integer.parseInt(rows) * (Integer.parseInt(page) - 1),
					Integer.parseInt(rows));
		}
		
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		jsonMap.put("total", empService.getSum());//total键 存放总记录数，必须的  

        jsonMap.put("rows", list);//rows键 存放每页记录 list  

        JSONObject result = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject
        out = res.getWriter();
		out.write(result.toString());
		System.out.println(result.toString());
		out.flush();
		out.close();
	}
}
